//
//  DTViewController.m
//  ObjcGoNew
//
//  Created by 邢伟新 on 2021/4/28.
//

#import "DTViewController.h"
#import "DTPerson.h"
#import "DTStudent.h"
#import "ColorCell.h"
#import "BlueCell.h"
#import "RedCell.h"

static NSString *idenCell = @"idenCell";
static NSString *idenCell1 = @"idenCell1";
static NSString *idenCell2 = @"idenCell2";



@interface DTViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,copy)NSArray *array;
@end

@implementation DTViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"多态";
    self.view.backgroundColor = [UIColor whiteColor];
    self.array = @[@"1",@"2",@"3",@"4",@"5",@"6"];
    [self baseUse];
    [self _initTableViews];
}


/*
 多态:父类指针指向子类对象
 父类指针指向多个子类对象,多个子类对象有着不同的表现形式
 */
-(void)baseUse{
    
    DTPerson *person = [[DTPerson alloc]init];
    DTStudent *stu = [[DTStudent alloc]init];
    DTPerson *stu1 = [[DTStudent alloc]init];
    
    [person fun];
    [stu fun];
    [stu1 fun];
}

-(void)_initTableViews{
    
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}

#pragma mark - 协议方法UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ColorCell *cell = [tableView dequeueReusableCellWithIdentifier:idenCell];
    if (cell == nil) {
        
        switch (indexPath.row) {
            case 0:
                cell = [[ColorCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:idenCell];
                break;
            case 1:
                cell = [[RedCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:idenCell];
                break;
            case 2:
                cell = [[BlueCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:idenCell];
                break;
                
                
            default:
                cell = [[ColorCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:idenCell];
                break;
        }
        
    }
    cell.textContent = self.array[indexPath.row];
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

#pragma mark -UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
}




@end
